﻿using ModernShopping.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModernShopping.User
{
    public partial class ProductList : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            LoginInfo loginInfo = Session.GetLoginInfo();
            if (loginInfo == null)
            {
                Response.GoToAccessDeniedPage(Request);
            }
            if (!loginInfo.IsUser())
            {
                Response.GoToAccessDeniedPage(Request);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                refreshDatalist();
            }
        }

        private void refreshDatalist()
        {
            DbProduct.initDatalist(dlProduct, CollectionPager, Session.GetLoginInfo().UserID);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            int ProductCode = int.Parse(button.CommandArgument);
            Response.GoToUpdateProductPage(Request, ProductCode);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Countdown.ShowPopup();
            Button button = sender as Button;
            int ProductCode = int.Parse(button.CommandArgument);
            if (DbProduct.Delete(ProductCode))
            {
                Countdown.InitCountdown(1, "Đã xóa sản phẩm");
                Countdown.Start();
                refreshDatalist();
            }
            else
            {
                Countdown.HidePopup();
            }

        }

        protected void btnDownloadQrCode_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            int ProductCode = int.Parse(button.CommandArgument);
            Response.DownloadQrCode(DbProduct.GetQrURL(ProductCode), ProductCode.ToString());
        }
    }
}